﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using rateITcd.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace rateITcd.Controllers
{
    public class DishReviewController : Controller
    {
        private RateItDB db = new RateItDB();

        // GET: DishReview
        public ActionResult Index()
        {
            List<String> accounts = new List<String>();
            ViewBag.Reviews = db.Reviews.ToList().OrderByDescending(x => x.Id).Take(5);
            // replace the userid of the review (Reviewer) with the name of the Reviewer
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            foreach (var item in db.Reviews.ToList())
            {
                if (item.Reviewer == null)
                {
                    item.Reviewer = "";
                }
                item.Reviewer = (manager.FindById(item.Reviewer).UserName);
            }
            return View();
        }

        // GET: DishReview/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RestaurantDish restaurantDish = db.Dishes.Find(id);
            List<String> accounts = new List<String>();

            if (restaurantDish == null)
            {
                return HttpNotFound();
            }
            ViewBag.Reviews = restaurantDish.Reviews.ToList();
            ViewBag.Title = restaurantDish.Name;
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new
                                               ApplicationDbContext()));
            foreach(var item in restaurantDish.Reviews.ToList()){
                var user = manager.FindById(item.Reviewer);
                item.Reviewer = user.UserName;
            }
            ViewBag.Id = id;
            return View();
        }

        // GET: DishReview/Create
        public ActionResult Create(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            if (User.IsInRole("Admin") || User.IsInRole("User"))
            {
                ViewBag.DishId = id;
                return View();
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // POST: DishReview/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Reviewer,Score,Comment")] DishReview dishReview, int? dishid)
        {
            if (User.IsInRole("Admin") || User.IsInRole("User"))
            {
                if (ModelState.IsValid)
                {
                    dishReview.Reviewer = User.Identity.GetUserId();
                    RestaurantDish dish = db.Dishes.Find(dishid);
                    dish.Reviews.Add(dishReview);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(dishReview);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // GET: DishReview/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DishReview dishReview = db.Reviews.Find(id);
            if (User.IsInRole("Admin") || (User.IsInRole("User") && dishReview.Reviewer == User.Identity.GetUserId()))
            {

                if (dishReview == null)
                {
                    return HttpNotFound();
                }
                return View(dishReview);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // POST: DishReview/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Reviewer,Score,Comment")] DishReview dishReview)
        {


            if (User.IsInRole("Admin"))
            {
                if (ModelState.IsValid)
                {
                    db.Entry(dishReview).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(dishReview);
            }
            else
            {
                return new HttpUnauthorizedResult();

            }
            //var reviewer = db.Reviews.Find(dishReview.Id).Reviewer;
            //if (User.IsInRole("Admin") || (User.IsInRole("User") && reviewer == User.Identity.GetUserId()))
            //{
            //    if (ModelState.IsValid)
            //    {
            //        db.Entry(dishReview).State = EntityState.Modified;
            //        //dishReview.Reviewer = reviewer;
            //        dishReview.Reviewer = User.Identity.GetUserId();
            //        db.SaveChanges();
            //        return RedirectToAction("Index");
            //    }
            //    return View(dishReview);
            //}
            //else
            //{
            //    return new HttpUnauthorizedResult();
            //}
        }

        // GET: DishReview/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DishReview dishReview = db.Reviews.Find(id);
            if (User.IsInRole("Admin") || (User.IsInRole("User") && dishReview.Reviewer == User.Identity.GetUserId()))
            {
                if (dishReview == null)
                {
                    return HttpNotFound();
                }
                return View(dishReview);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // POST: DishReview/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            DishReview dishReview = db.Reviews.Find(id);
            if (User.IsInRole("Admin") || (User.IsInRole("User") && dishReview.Reviewer == User.Identity.GetUserId()))
            {
                db.Reviews.Remove(dishReview);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
